from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
from selenium.webdriver.support import expected_conditions as EC
import unittest, time


class DZPY3(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://localhost/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_d_z_p_y3(self):
        driver = self.driver
        wait = WebDriverWait(driver, 30)
                # LOGIN
        driver.get(self.base_url + "/php4dvd/")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("admin")
        driver.find_element_by_name("password").clear()
        driver.find_element_by_name("password").send_keys("admin")
        driver.find_element_by_name("submit").click()
            # Find Search Form
        element = wait.until(
            lambda x: x.find_element(By.XPATH, "//*[@id='q']"))

                # ADD INVALID MOVIE
        driver.get(self.base_url + "/php4dvd/")
        driver.find_element_by_xpath("//*[@id='content']/section/nav/ul/li[1]/div/div/a").click()
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys("")
        driver.find_element_by_name("year").clear()
        driver.find_element_by_name("year").send_keys("")
            # Select text in 'format' and clear delete all text
        element = driver.find_element_by_id("formats")
        element.send_keys(Keys.CONTROL, "a")
        time.sleep(1)
        element.send_keys(Keys.DELETE)
        time.sleep(1)
        driver.find_element_by_id("submit").click()
            # Check validation error input 'name'
        element = wait.until(
            lambda x: x.find_element(By.CSS_SELECTOR, ".required.error"))
            # Check validation error input 'year'
        element = wait.until(
            lambda x: x.find_element(By.CSS_SELECTOR, ".required.digits.error"))
            # Check validation error input 'formats'
        element = wait.until(
            lambda x: x.find_element(By.XPATH, "//*[@id='updateform']/table/tbody/tr[7]/td[2]/label"))

                # ADD VALID MOVIE
        driver.get(self.base_url + "/php4dvd/")
        driver.find_element_by_xpath("//*[@id='content']/section/nav/ul/li[1]/div/div/a").click()
        driver.find_element_by_name("name").clear()
        driver.find_element_by_name("name").send_keys("New movie")
        driver.find_element_by_name("year").clear()
        driver.find_element_by_name("year").send_keys("2013")
        driver.find_element_by_id("loaned_yes").click()
        driver.find_element_by_id("loaned_no").click()
        driver.find_element_by_id("cover").send_keys("C:\selenium_and_py\selenium-py-training-vasiliev\selenium-py-training-vasiliev\php4dvd\JPG1.jpg")
        driver.find_element_by_id("submit").click()
            #  Find button delete
        element = wait.until(
            lambda x: x.find_element(By.XPATH, "//*[@id='content']/section/nav/ul/li[4]/div/div/a"))

            #  SELECT MOVIE
        driver.get(self.base_url + "/php4dvd/")
            # Find Movie from alt="New movie"
        element = wait.until(
            lambda x: x.find_element(By.CSS_SELECTOR, "img[alt=\"New movie\"]"))
        driver.find_element_by_css_selector("img[alt=\"New movie\"]").click()

                # DELETE MOVIE
        driver.find_element_by_xpath("//*[@id='content']/section/nav/ul/li[5]/div/div/a").click()
        self.assertRegexpMatches(self.close_alert_and_get_its_text(), r"^[\s\S]*$")
            # Check Movie from alt="New movie"
    def wait_invisibility_of_element_located_by(locator_type, locator_value):
        driver = webdriver.Firefox()
        wait = WebDriverWait(driver, 10)
        base_url = "http://localhost/"
        elements = driver.find_elements(By.CSS_SELECTOR, "img[alt=\"New movie\]")
        if len(elements) == 0:
                return
        element = elements[0]
        wait.until(EC.staleness_of(element))

                #  LOGOUT
        driver.get(base_url + "/php4dvd/")
        driver.find_element_by_xpath("//*[@id='wrapper']/header/div/nav/ul/li[4]/a").click()
        self.assertRegexpMatches(self.close_alert_and_get_its_text(), r"^[\s\S]*$")
            # Check Search Form
    def wait_invisibility_of_element_located_by(locator_type, locator_value):
        driver = webdriver.Firefox()
        wait = WebDriverWait(driver, 10)
        elements = driver.find_elements(By.XPATH, "//*[@id='q']")
        if len(elements) == 0:
                return
        element = elements[0]
        wait.until(EC.staleness_of(element))

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
